package HLLib.ui;

import HLCode.HLClass;
import HLCode.HLClassManager;
import HLCode.HLClassManager_H;
import HLCode.HLObject;
import HLLib.base.HLGraphics;
import HLLib.base.HLGraphics_H;
import HLLib.base.HLRectangle;
import HLLib.game.HLSprite;
import HLLib.io.HLFile;

public class HLUIControlSprite extends HLUIControl {
    public HLClass GetClass(HLClassManager classManager) {
        return classManager.GetLibClass(HLClassManager_H.CLASS_GROUP_TYPE_LIB_UI, HLClassManager_H.LIB_CLASS_UI_UIControlSprite);
    }

    public int GetInt(int index) {
        return super.GetInt(index);
    }

    public void SetInt(int index, int value) {
        super.SetInt(index, value);
    }

    public boolean GetBoolean(int index) {
        switch (index) {
        case 1: {
            return autoNextFrame;
        }
        }
        return super.GetBoolean(index);
    }

    public void SetBoolean(int index, boolean value) {
        switch (index) {
        case 1: {
            autoNextFrame = value;
            return;
        }
        }
        super.SetBoolean(index, value);
    }

    public HLObject GetObject(int index) {
        switch (index) {
        case 0: {
            return sprite;
        }
        }
        return super.GetObject(index);
    }

    public void SetObject(int index, HLObject value) {
        switch (index) {
        case 0: {
            sprite = (HLSprite) value;
            return;
        }
        }
        super.SetObject(index, value);
    }

    public HLSprite sprite;

    public boolean autoNextFrame = true;

    public HLUIControlSprite() {
    }

    HLUIControlSprite(HLUIFrame ownerFrame) {
        super(ownerFrame);
    }

    void Load(HLFile file) {
        super.Load(file);

        short spriteIndex = file.ReadInt16();
        byte animationIndex = file.ReadInt8();
        byte trans = file.ReadInt8();
        ownerFrame.owner.ownerUISeedManager.spriteSeedManager.Load(spriteIndex);
        sprite = ownerFrame.owner.ownerUISeedManager.spriteSeedManager.CreateSprite(spriteIndex);

        sprite.SetCurAnimateFrame(animationIndex, 0);

        sprite.SetTrans(trans);
    }

    void Render(HLGraphics g, int rectX, int rectY, int rectWidth, int rectHeight, int color) {
        if (sprite != null) {
            HLRectangle area = GetArea(rectX, rectY, rectWidth, rectHeight);
            int oldColor = sprite.color;
            sprite.color = HLGraphics.ColorMultiply(color, oldColor);
            sprite.RenderAdjustable(g, area.x, area.y, area.width, area.height, HLGraphics_H.HCENTER_VCENTER);
            sprite.color = oldColor;
            if (autoNextFrame) {
                sprite.NextCurFrameCyc();
            }
        }
    }

    void CopyFrom(HLUIControl uiControl) {
        super.CopyFrom(uiControl);

        HLUIControlSprite uiControlByType = (HLUIControlSprite) uiControl;

        sprite = HLSprite.Create(uiControlByType.sprite.seed);

        autoNextFrame = uiControlByType.autoNextFrame;
    }

    HLUIControl Clone(HLUIFrame ownerFrame) {
        HLUIControlSprite uiControl = new HLUIControlSprite();
        uiControl.ownerFrame = ownerFrame;
        uiControl.CopyFrom(this);
        return uiControl;
    }
}
